﻿using LJ.Abstractions.Extensions;
using LJ.Abstractions.Reflection;

namespace LJ.DataAccess.EntityFrameworkCore.Internal;

public class EFDataObjectMapManager : IDataObjectMapManager
{
    private readonly IReflectionManager _reflectionManager;

    private readonly HashSet<IDataObjectMap> _dataObjectMaps = [];

    public EFDataObjectMapManager(IReflectionManager reflectionManager)
    {
        _reflectionManager = reflectionManager;


        var types = FindTypes();
        foreach (var type in types)
        {
            var dataObjectMap = (IDataObjectMap)Activator.CreateInstance(type)!;
            _dataObjectMaps.Add(dataObjectMap);
        }
    }

    private List<Type> FindTypes()
    {
        Type[] baseTypes = [typeof(IDataObjectMap)];
        return _reflectionManager.GetTypes(type => baseTypes.Any(b => type.IsDeriveClassFrom(b)));
    }

    public IReadOnlyCollection<IDataObjectMap> GetDataObjectMaps()
    {
        return [.. _dataObjectMaps];
    }
}
